//
//  WHYProgressImageView.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/12/3.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYProgressImageView: UIImageView {
    
    /// 进度
    var progress: CGFloat = 0 {
        didSet {
            progressView.progress = progress
        }
    }

//    MARK: - 构造函数
    init() {
        super.init(frame: CGRectZero)
        
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()
        
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - 懒加载子控件
    private lazy var progressView: WHYProgressView = WHYProgressView()
    

}

// MARK: - 进度视图
/// 进度视图
private class WHYProgressView: UIView {
    /// 进度
    var progress: CGFloat = 0 {
        didSet{
            setNeedsDisplay()
        }
    }
    
    override private func drawRect(rect: CGRect) {
        
        let center = CGPoint(x: rect.size.width * 0.5, y: rect.size.height * 0.5)
        let r = min(rect.size.width, rect.size.height) * 0.5
        let start = CGFloat(-M_PI_2)
        let end = start + progress * CGFloat(M_PI) * 2
        
        let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
        path.addLineToPoint(center)
        path.closePath()
        
        UIColor(white: 1, alpha: 0.5).setFill()
        
        path.fill()
    }
}
